package com.echoesnet.echoesshortvideo.View;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.echoesnet.echoesshortvideo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lc on 2017/7/20 17.
 */

public class BottomSelector extends PopupWindow
{
    private final String TAG = BottomSelector.class.getSimpleName();

    private Activity mContext;
    private final int[] mLocation = new int[2];
    private TextView tv1,tv2,tv3,tv4,tvCancel;

    private List<String> mActionDes;
    private List<TextView> mActionItems=new ArrayList<>();

    public BottomSelector(Activity context, List<String> list)
    {
        this(context,list, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public BottomSelector(Activity context, List<String> list, int width, int height)
    {
        this.mContext = context;
        this.mActionDes = list;

        //设置可以获得焦点
        setFocusable(true);
        //设置弹窗内可点击
        setTouchable(true);
        //设置弹窗外可点击
        setOutsideTouchable(true);
        //获得屏幕的宽度和高度
        //mScreenWidth = CommonUtils.getScreenWidth(mContext);
        //mScreenHeight = CommonUtils.getScreenHeight(mContext);

        //设置弹窗的宽度和高度
        this.setAnimationStyle(R.style.anim_menu_bottombar);
        setWidth(width);
        setHeight(height);
        setBackgroundDrawable(new ColorDrawable(0));
        setOnDismissListener(new OnDismissListener()
        {
            @Override
            public void onDismiss()
            {
                BottomSelector.this.backgroundAlpha(1f);
            }
        });
        initUI();
    }

    /**
     * 初始化弹窗列表
     */
    private void initUI()
    {
        //设置弹窗的布局界面
        setContentView(LayoutInflater.from(mContext).inflate(R.layout.my_infor_popup, null));
        tv1 = (TextView) getContentView().findViewById(R.id.tv_1);
        tv2 = (TextView) getContentView().findViewById(R.id.tv_2);
        tv3 = (TextView) getContentView().findViewById(R.id.tv_3);
        tv4 = (TextView) getContentView().findViewById(R.id.tv_4);
        tvCancel = (TextView) getContentView().findViewById(R.id.tv_cancel);
        TextView[] btns={tv1,tv2,tv3,tv4};
        if (mActionDes.size()>4)
            throw new IllegalArgumentException("集合的最大长度为4");
        for (int i = 0; i < btns.length; i++)
        {
            btns[i].setVisibility(View.GONE);
        }
        mActionItems.clear();
        for (int i = 0; i < mActionDes.size(); i++)
        {
            btns[i].setText(mActionDes.get(i));
            btns[i].setVisibility(View.VISIBLE);
            mActionItems.add(btns[0]);
        }

        tvCancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
               dismiss();
            }
        });
    }

    public List<TextView> getActionItems()
    {
        return mActionItems;
    }
    /**
     * 显示弹窗列表界面
     */
    public void show(View view)
    {
        this.backgroundAlpha(0.5f);
        //获得点击屏幕的位置坐标
        view.getLocationOnScreen(mLocation);
        //设置矩形的大小
     //   mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(), mLocation[1] + view.getHeight());
        //显示弹窗的位置
        showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
//        showAtLocation(view, popupGravity, mScreenWidth - LIST_PADDING - (getWidth()/2), mRect.bottom);
    }

    public BottomSelector setFirstClickListener(OnItemClickListener listener)
    {
        tv1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (listener!=null)
                    listener.onItemClick(v,BottomSelector.this);
            }
        });
        return this;
    }

    public BottomSelector setSecondClickListener(OnItemClickListener listener)
    {
        tv2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (listener!=null)
                    listener.onItemClick(v,BottomSelector.this);
            }
        });
        return this;
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0-1.0
        mContext.getWindow().setAttributes(lp);
    }

    public interface OnItemClickListener
    {
       void onItemClick(View view,BottomSelector pop);
    }

}
